package com.liao.myblog.controller.admin;

import com.liao.myblog.enums.ResultCode;
import com.liao.myblog.pojo.Blog;
import com.liao.myblog.pojo.Catalogue;
import com.liao.myblog.service.BlogService;
import com.liao.myblog.service.CatalogueService;
import com.liao.myblog.vo.BlogPage;
import com.liao.myblog.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RequestMapping(value = "/admin")
@Controller
public class BlogController {
    @Resource
    private BlogService blogService;

    @Resource
    private CatalogueService catalogueService;

    @GetMapping(value = "/blogs")
    public String index(HttpServletRequest request) {
        request.setAttribute("path", "blogs");
        return "/admin/blog";
    }

    @ResponseBody
    @GetMapping(value = "/blogs/list")
    public BlogPage blogsList(@RequestParam Map<String, Object> map) {
        int page = Integer.parseInt(map.get("page").toString());
        int rows = Integer.parseInt(map.get("rows").toString());
        return blogService.queryBlogByKeyword(rows, page, "");
    }

    @GetMapping(value = "/blogs/edit")
    public String edit(HttpServletRequest request) {
        request.setAttribute("path", "edit");
        request.setAttribute("catalogues", catalogueService.findCatalogueList());
        return "admin/edit";
    }

    @GetMapping(value = "/blogs/edit/{id}")
    public String edit(HttpServletRequest request, @PathVariable(value = "id") int id) {
        request.setAttribute("path", "edit");
        Blog blog = blogService.queryBlogById(id);
        if (null == blog) {
            return "error/error_404";
        }
        request.setAttribute("catalogues", catalogueService.findCatalogueList());
        request.setAttribute("blog", blog);
        return "admin/edit";
    }

    @ResponseBody
    @PostMapping(value = "/blogs/save")
    public ResultVO<Object> save(@RequestBody Blog blog) {
        if (blog.getTitle().isEmpty()) {
            return new ResultVO<>(ResultCode.VALIDATE_FAILED, "标题不能为空!");
        }
        if (blog.getContent().isEmpty()) {
            return new ResultVO<>(ResultCode.VALIDATE_FAILED, "内容不能为空!");
        }
        blog.setIsDelete((byte) 0);
        blog.setTime(new Date());
        blog.setViews(0);
        boolean b = blogService.saveBlog(blog);
        if (b) {
            return new ResultVO<>(ResultCode.SUCCESS);
        }
        return new ResultVO<>(ResultCode.ERROR);
    }


    @ResponseBody
    @PostMapping(value = "/blogs/update")
    public ResultVO<Object> update(@RequestBody Blog blog) {
        if (blog.getTitle().isEmpty()) {
            return new ResultVO<>(ResultCode.VALIDATE_FAILED, "标题不能为空!");
        }
        if (blog.getContent().isEmpty()) {
            return new ResultVO<>(ResultCode.VALIDATE_FAILED, "内容不能为空!");
        }
        Blog blog1 = blogService.queryBlogById(blog.getId());
        blog1.setTitle(blog.getTitle());
        blog1.setContent(blog.getContent());
        blog1.setCatalogue_id(blog.getCatalogue_id());
        blog1.setIcon(blog.getIcon());
        boolean b = blogService.updateBlogById(blog1);
        if (b) {
            return new ResultVO<>(ResultCode.SUCCESS);
        }
        return new ResultVO<>(ResultCode.ERROR);
    }

    @ResponseBody
    @PostMapping(value = "/blogs/delete")
    public ResultVO<Object> delete(@RequestBody Map<String, int[]> map) {

        int[] ids = map.get("ids");

        int i = blogService.deleteBlog(ids);
        if (i < 1) {
            return new ResultVO<>(ResultCode.ERROR);
//            return ResultUtil.getResult("删除失败", ResultUtil.RESULT_ERROR);
        }
        return new ResultVO<>(ResultCode.SUCCESS);
//        return ResultUtil.getResult("删除成功", ResultUtil.RESULT_SUCCESS);
    }

    @ResponseBody
    @GetMapping(value = "/blogs/list/{title}")
    public List<Blog> queryBlogByTitle(@PathVariable(value = "title") String title) {
        List<Blog> blogs = blogService.queryBlogByTitle(title);
        for (Blog blog : blogs) {
            Catalogue catalogue = catalogueService.findCatalogueById(blog.getCatalogue_id());
            blog.setCatalogue_name(catalogue.getName());
        }
        return blogs;
    }

}
